#include "WorldModel.h"

WorldModel::WorldModel()
{
}
WorldModel::~WorldModel()
{
	for( std::vector<WorldModelNode*>::iterator wmnit = m_WMNodes.begin();
		wmnit != m_WMNodes.end(); wmnit ++ )
	{
		WorldModelNode* wmn = *wmnit;
		if( wmn )
		{
			delete wmn;
			wmn = NULL;
		}
	}
}
void WorldModel::ParseLog( const std::string& name )
{
	time_t t1,t2;
	time(&t1);

	std::cout<< "(WorldModel) " << "Parsing log...\n";
	if( !m_LogParser.MonitorLogParse( name, m_WMNodes ) )
	{
		std::cerr<< "(WorldModel)[Error] " << "MonitorLogParse" << "\n";
		exit(-1);
	}

	UpdateOmittedInfo();

	std::cout<< "(WorldModel) " << "Parsing log done\n";

	time(&t2);
	std::cout<<"(WorldModel) Time used: " << difftime(t2,t1) << "s \n";
}
unsigned int WorldModel::Size()
{
	return m_WMNodes.size();
}
WorldModelNode* WorldModel::WM( int idx )
{
	if( idx < 0 )
	{
		idx = 0;
	}
	else if( idx > Size() - 1 )
	{
		idx = Size() - 1;
	}

	return m_WMNodes[idx];
}
std::vector<WorldModelNode*>& WorldModel::WMVec()
{
	return m_WMNodes;
}
void WorldModel::UpdateOmittedInfo()
{
	WorldModelNode* prevWMNode = m_WMNodes[0];
	for( unsigned int i = 1; i < m_WMNodes.size(); i ++ )
	{
		WorldModelNode* currWMNode = m_WMNodes[i];
		if( currWMNode->mNeedLoadOmittedInfo )
		{
			currWMNode->LoadOmittedInfo( prevWMNode->GetRenderNode() );
		}
		else
		{
			prevWMNode = currWMNode;
		}
	}
}